﻿<div class="widget">
   <h3>Thông tin ca thi</h3>
</div>
<?php
        $idca=$_GET["maca"];
        if(isset($_POST["btnTao"])){
           if(isset($_POST['txtCa']) && isset($_POST['txtGio']) && isset($_POST['txtPhut'])){ 
                $cathi="Ca ".$_POST["txtCa"];
                 $gio=$_POST["txtGio"];
                 if(strlen($gio)==1){
                    $gio="0".$gio;
                 }
               
                $phut=$_POST["txtPhut"];
                if(strlen($phut)==1){
                    $phut="0".$phut;
                 }
                 
                $giothi="$gio:$phut";
                $sql="INSERT INTO `cathi` VALUES('','$cathi','$giothi')"; 
                
                
                if(is_numeric($_POST["txtCa"])){
                     $rs=mysql_query($sql);
                     if(rs){
                           echo "<script>alert('Tạo ca thi thành công')</script>"; 
                     }else{
                        
                         echo "<script>alert('Tạo ca thi thất bại')</script>"; 
                     }
                }
                else{
                    echo "<script>alert('Ca thi phải là số')</script>";
                    
                }
            }else{
                echo "<script>alert('Bạn phải nhập đầy đủ thông tin')</script>";
                
            }
        }
        if(isset($_POST["btnSua"])){
            $cathi="Ca ".$_POST["txtCa"];
            $gio=$_POST["txtGio"];
            if(strlen($gio)==1){
                    $gio="0".$gio;
                 }
            $phut=$_POST["txtPhut"];
            if(strlen($phut)==1){
                    $phut="0".$phut;
                 }
            $giothi="$gio:$phut";
            if(is_numeric($_POST["txtCa"])){
                $sql3="Update `cathi`  set `tenca`='$cathi',`gio`='$giothi' where `idca`=$idca";
                $rs3=mysql_query($sql3);
                if($rs3){
                    echo "<script>alert('Cập nhật thành công!!')</script>";   
                    
                }else{
                     echo "<script>alert('Cập nhật thất bại do trùng ca thi!!')</script>";   
                    
                }
                
            }
            
            else{
                echo "<script>alert('Cập nhật thất bại ca thi phải là số!!')</script>";   
                
            }   
        }
 ?>
 <form action="" method="post" onsubmit="return checkData()">
         <fieldset>
           <legend>Tạo ca thi</legend>
          <?php
            $sql2="SELECT * from `cathi` where `idca`=$idca";
            $rs2=mysql_query($sql2);
            $r2=mysql_fetch_assoc($rs2);
            //echo $sql2;
           
          
           ?>
            <table>
                <tr>
                  <td align="center">Ca thi:</td>
                  <td>
                    <input type="text" size="3" value="<?php echo substr($r2["tenca"],3); ?>" name="txtCa" id="txtCa"/>
                  </td>
                </tr>
                 <tr>
                  <td align="center">Giờ thi:   </td>
                   <td> Giờ :<input type="text" name="txtGio" id="txtGio" value="<?php if(strlen($r2["gio"])==4){ echo substr($r2["gio"],0,1);}else{ echo substr($r2["gio"],0,2);} ?>" size="4"/> : Phút <input type="text" value="<?php 
                    if(strlen($r2["gio"])==4){
                        echo substr($r2["gio"],2,2);
                        }
                        else{
                            echo substr($r2["gio"],3,2);
                            
                        }
                   ?> " name="txtPhut" id="txtPhut" size="4" /></td>
                </tr>
                <tr>
               
                </tr>
                <tr>
                    <td colspan="2" align="center">
                    	<input type="submit" name="btnTao" value="Tạo Ca Thi"/>
                        <input type="submit" name="btnSua" value="Cập nhật"/>
                        
                    </td>
                </tr>
                </table>
          </fieldset>
</form>
<div class="widget">
   <h3>Danh sách phòng thi</h3>
</div>
 <?php
    $sort=$_GET['sort'];
    $p=$_GET['page'];
    $sql="SELECT * FROM `cathi`";
    $sqlcount="SELECT count(*) FROM `cathi`";
    $noi=0;
    echo "<br>";
    if($sort==1){ //ca thi moi
        $sql.=" ORDER BY `idca` DESC";
        
    }
    if($sort==2){ //ca thi cu
        $sql.=" ORDER BY `idca` ASC";
        
    }
    
    //echo $sqlcount;
    $rscount=mysql_query($sqlcount);
    $rcount=mysql_fetch_row($rscount);
    $noi=$rcount[0];
   // echo $noi;
     $p=max(0,round($p));
     if(!isset($_GET['page']))
			{
				$_GET['page'] = 1;
			}
   $ipp=10;
   
   $nop=ceil($noi/$ipp);
        
        $p=min($nop,$p);	
        $pos=($p-1)*$ipp;
   if($p>=1){
           
            $sql.=" LIMIT $pos,$ipp";
         
        }
        //echo $sql;
 
  ?>
  <p style="margin-left:150px ; margin-bottom: 20px;">
  Sắp xếp theo : <select onchange="window.location='?mod=quanlycathi&sort='+this.value+'&page=<?php echo $p?>'">
    <option <?php if($sort==0)echo 'selected'; ?> value="0">--Sắp xếp ca thi theo--</option>
    <option <?php if($sort==1)echo 'selected'; ?> value="1">Ca thi mới</option>
    <option <?php if($sort==2)echo 'selected'; ?> value="2">Ca thi cũ</option>
  </select>
  </p>
<form action="" method="post">
<table border="1"  style="width: 500px;" >
 <!-- begin caption -->
	<tr height="30">
    	<th>STT</th>
        <th>Tên ca</th>
        <th>Giờ thi</th>
        <th>Công Cụ</th>
    <tr>
    <!-- end caption -->
  
  <?php
		$rs=mysql_query($sql);
		$count=0;
		while($r=mysql_fetch_assoc($rs)){
		$count++;
	 ?>
     <tr style="background-color: <?php if($count%2!=0) echo "#CCC";  ?>">
        <td align="center"><?php echo $count ?></td>
        <td align="center"><?php echo $r["tenca"] ?></td>
        <td align="center"><?php 
               echo $r["gio"];
        
         ?></td>
         <td align="center"><a href="?mod=quanlycathi&sort=<?php echo $sort ?>&page=<?php echo $p?>&maca=<?php echo $r["idca"]; ?>"><img src="images/ico_edit.png" style="width: 25px;height: 25px;"/></a>|<a onclick="return confirm('Bạn có chắc chắn muốn xóa')"   href="?mod=xoacathi&maca=<?php echo $r['idca']?>"><img src="images/ico_delete.png" style="width: 25px;height: 25px;" /></a></td>
        
     
     <?php } ?>
     </tr>
     </table>
   <?php
		 
           
			echo '<div id="center2">';
			echo '<ul id="pagination-digg">';
			if($p > 1)
			{
				echo '<li class="previous"><a href="?mod=quanlyphongthi&sort='.$sort.'&page='.($p-1).'">Back</a></li>';
			}
			for($i=1;$i<$nop;$i++)
			{
				if($p == $i)
				{
					echo '<li class="active">'.$i.'</li>';
				}
				else
				{
					echo '<li><a href="?mod=quanlyphongthi&sort='.$sort.'&page='.$i.'">'.$i.'</a></li>';
				}
			}
			if($p < $nop)
			{
				echo '<li class="next"><a href="?mod=quanlyphongthi&sort='.$sort.'&page='.($p+1).'">next</a></li></ul></div>';
			}
			else
			{
				echo '<li class="next-off">next</li></ul></div>';
			}
		 ?>       
           <script>
                           
							
							function checkData()
							{
								obj=document.getElementById('txtCa');
								if(isNaN(obj.value)==true)
								{
									alert('Ca thi phải là số!');
									obj.focus();
									return false;
								}
       	                        obj=document.getElementById('txtGio');
								if(isNaN(obj.value)==true)
								{
									alert('Giờ thi phải là số!');
									obj.focus();
									return false;
								}
       	                        obj=document.getElementById('txtPhut');
                                if(isNaN(obj.value)==true)
								{
									alert('Phút phải là số!');
									obj.focus();
									return false;
								}
							
							
							
								
							}


</script>            
     
    